<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Working with images</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Ruby, SQLite, images, database, sqlite3">
<meta name="description" content="In this part of the SQLite Ruby tutorial, 
we work with images. We insert an image to the database and retrieve it back.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Working with images</h1>

<p>
In this chapter of the SQLite Ruby tutorial, we will work with image files.
Note that some people oppose putting images into databases. Here we 
only show how to do it. We do not dwell into technical issues of whether to save images in
databases or not. 
</p>

<pre>
sqlite> CREATE TABLE Images(Id INTEGER PRIMARY KEY, Data BLOB);
</pre>

<p>
For this example, we create a new table called Images. For the images, we use
the <code>BLOB</code> data type, which stands for Binary Large Object. 
</p>

<div class="center">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* top_horizontal */
google_ad_slot = "3327173442";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<h2>Inserting images</h2>

<p>
In the first example, we are going to insert an image to the 
SQLite database. 
</p>

<pre class="code">
#!/usr/bin/ruby

require 'sqlite3'

begin
    
    fin = File.open "woman.jpg" , "rb"
    img = fin.read
    
rescue SystemCallError => e      
    puts e
ensure
    fin.close if fin 
end

begin
    
    db = SQLite3::Database.open 'test.db'
    blob = SQLite3::Blob.new img
    db.execute "INSERT INTO Images VALUES(1, ?)", blob
    
rescue SQLite3::Exception => e 
    
    puts "Exception occured"
    puts e
    
ensure
    db.close if db
end
</pre>

<p>
We read an image from the current working directory 
and write it into the Images table of the SQLite test.db database. 
</p>

<pre class="explanation">
fin = File.open "woman.jpg" , "rb"
img = fin.read
</pre>

<p>
We open and read a jpg image. The <code>read</code> method returns the 
data as string.
</p>

<pre class="explanation">
blob = SQLite3::Blob.new img
</pre>

<p>
We create an instance of the <code>SQLite3::Blob</code> class. It
is intended for working with binary data. 
</p>

<pre class="explanation">
db.execute "INSERT INTO Images VALUES(1, ?)", blob
</pre>

<p>
The image is written to the database. 
</p>


<h2>Reading images</h2>

<p>
In this section, we are going to perform the reverse operation.
We will read an image from the database table. 
</p>

<pre class="code">
#!/usr/bin/ruby

require 'sqlite3'

begin
    
    db = SQLite3::Database.open 'test.db'   
    data = db.get_first_value "SELECT Data FROM Images LIMIT 1"    

    f = File.new "woman2.jpg", "wb"
    f.write data

rescue SQLite3::Exception, SystemCallError => e 
    
    puts "Exception occured"
    puts e
    
ensure
    f.close if f
    db.close if db
end
</pre>

<p>
We read image data from the Images table and write it
to another file, which we call woman2.jpg. 
</p>

<pre class="explanation">
data = db.get_first_value "SELECT Data FROM Images LIMIT 1"  
</pre>

<p>
This line selects the image data from the table.
</p>

<pre class="explanation">
f = File.new "woman2.jpg", "wb"
f.write data
</pre>

<p>
We open a new image file and write the retrieved data
into that file. Then we close the file.  
</p>

<p>
This part of the SQLite Ruby tutorial was dedicated to reading and writing
images. 
</p>

<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>

<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified April 10, 2012  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
